package com.yueya.business.comment;

import java.util.HashMap;
import java.util.Map;
import com.jfinal.aop.Before;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.comment.model.Comment;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.util.DateFormat;

/**
 * 
 * @author yangfeng
 *
 */
public class CommentController extends BaseController {

	Map<String, String> map = new HashMap<String, String>();

	public void index() {
		String refId = this.getPara("refid");
		setAttr("comment", Comment.service.getList(refId));
		render("/WEB-INF/views/business/comment/list.jsp");
	}

	public void getAllListPager() {
		map.clear();
		map.put("ref_id", this.getPara("refid"));
		Page<Comment> page = Comment.service.pagination(this.getPage(), this.getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void doEdit() {
		Comment comment = Comment.dao.findById(this.getPara("id"));
		this.setAttr("model", comment);
		// this.setAttr("visit", PropKit.get("upload.visit"));
		render("/WEB-INF/views/business/test/test_edit.jsp");
	}

	// 操作保存
	public void save() {
		boolean bl = false;
		Comment test = this.getModel(Comment.class);
		if (StrKit.notBlank(test.getStr("comment_id"))) {
			test.set("update_time", DateFormat.getDate("yyyy-MM-dd HH:mm:ss"));
			bl = test.update();
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("提交成功");
		} else {
			j.setSuccess(false);
			j.setMsg("提交失败");
		}
		renderJson(j);
	}

	// 删除
	@Before(Tx.class)
	public void remove() {
		boolean bl = false;
		if (getPara("id") != null) {
			String id = this.getPara("id").replace("/", "").trim();
			bl = Comment.service.remove(id);
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	// 批量移除
	@Before(Tx.class)
	public void bathremove() {
		Boolean bl = false;
		String ids = this.getPara("ids");
		if (ids != null) {
			bl = Comment.service.bathremove(ids);
		}
		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

}